package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Contact;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.Page;
import com.kaishengit.util.RowMapper;

public class ContactDao {
	private DBHelp<Contact> db = new DBHelp<Contact>();
	public long getAccount(String id) {
		String sql = "select count(*) from t_contact where t_project_id = ?";
		return (Long) db.executeForFunction(sql, id);
		
	}
	public void edit(Contact contact){
		String sql = "UPDATE t_contact SET `name`=?,company=?,cellphone=?,telphone=?,email=?,address=?,mainpage=?,web=?,introduce=? WHERE id = ?";
		db.executeSQL(sql,contact.getName(),contact.getCompany(),contact.getCellphone(),contact.getTelphone(),contact.getEmail(),contact.getAddress(),contact.getMainpage(),contact.getWebUrl(),contact.getIntroduce(),contact.getId());
	}
	public Page<Contact> findAllContactByProjectId(Page<Contact> page, String projectid) {
		String sql="select id,name,company,cellphone,telphone,email,address,mainpage,web,introduce,t_project_id from t_contact where t_project_id  = ?";
		List<Contact> list =  db.executeQueryForList(sql, new ContactRowMapper(), projectid);	
		page.setList(list);
		return page;
	}
	private class ContactRowMapper implements RowMapper<Contact>{

		public Contact mapRow(ResultSet rs) throws SQLException {
			Contact contact = new Contact();
			contact.setAddress(rs.getString("address"));
			contact.setCellphone(rs.getString("cellphone"));
			contact.setTelphone(rs.getString("telphone"));
			contact.setCompany(rs.getString("company"));
			contact.setEmail(rs.getString("email"));
			contact.setId(rs.getString("id"));
			contact.setMainpage(rs.getString("mainpage"));
			contact.setIntroduce(rs.getString("introduce"));
			contact.setName(rs.getString("name"));
			contact.setProjectid(rs.getString("t_project_id"));
			String web1 = rs.getString("web");
			if(web1 != null){
				contact.setWebUrl(web1);
				String web2 = web1.substring(0,web1.lastIndexOf("/")+1);
				contact.setPrexweb(web2);
				String web = web1.substring(web1.lastIndexOf("/")+1);
				contact.setWeb(web);
			}
			
			
			
			return contact;
		}
		
	}
	public Contact findContact(String id) {
		String sql="select id,name,company,cellphone,telphone,email,address,mainpage,web,introduce,t_project_id from t_contact where id  = ?";
		return db.executeQueryForObject(sql, new ContactRowMapper(), id);
	}
	public void delByContactId(String id) {
		String sql = "delete from t_contact where id = ?";
		db.executeSQL(sql, id);
		
	}
	public void save(Contact contact) {
		String sql = "insert into t_contact(id,name,company,cellphone,telphone,email,address,mainpage,web,introduce,t_project_id) values(?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql,contact.getId(),contact.getName(),contact.getCompany(),contact.getCellphone(),contact.getTelphone(),contact.getEmail(),contact.getAddress(),contact.getMainpage(),contact.getWebUrl(),contact.getIntroduce(),contact.getProjectid() );
		
	}
	
}
